package tech.aistar.controller.noteinfo;

import org.omg.CORBA.NO_IMPLEMENT;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import sun.rmi.server.InactiveGroupException;
import tech.aistar.entity.authordis.NoteAuthor;
import tech.aistar.entity.authordis.Notes;
import tech.aistar.entity.authordis.NotesInfo;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.service.noteinfo.INoteInfoService;
import tech.aistar.service.noteinfo.INotePayService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.swing.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

/**
 * Created by lenovo on 2019/10/7.
 */
@Controller
@RequestMapping("/noteInfoDis")
public class NoteInfoDis {

    @Autowired
    private INoteInfoService noteInfoService;
    @Autowired
    private INotePayService notePayService;

    @GetMapping("/noteInDi")
    public String noteInDi() {
        return "noteinfo/noteinfo";
    }

    /**
     * 游记详情的辅助信息获取
     *
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/noteDis")
    public String noteDis(Model model, HttpServletRequest req, HttpSession session) {

        UserTable userLogin = (UserTable) session.getAttribute("userId");

        //获取点击的文章id
        Integer ids = Integer.valueOf(req.getParameter("id"));
        //通过获取的id获取游记信息
        Notes notes = noteInfoService.findNotesById(ids);

        //通过游记信息获取游记作者id获取作者信息
        Integer authorId = notes.getAutuorId();
        NoteAuthor noteAuthor = noteInfoService.findNoteAuthorById(authorId);

        //通过游记作者信息获取游记作者用户表id获取用户信息
        Integer userId = noteAuthor.getUserId();
        UserTable userTable = noteInfoService.findUserById(userId);

        //侧面相关数据查询 - 随机七个
        //通过获取的游记信息获取游记所属地点
        String notePlace = notes.getNotePlace();
        //通过地点查询相关游记
        List<Notes> notesList = noteInfoService.findRelNotes(notePlace);

        //定义一个集合保存随机选取的游记
        List<Notes> notesBack = new ArrayList<>();
        //随机数对象
        Random random = new Random();
        //判断查询的所有游记的集合大小 - 确定显示数量
        int backSum = 0;
        if (notesList.size() >= 7) {
            backSum = 7;
        } else {
            backSum = notesList.size();
        }
        //数组保存已产生的随机数
        Integer[] temp = new Integer[7];
        Arrays.fill(temp, 0);
        for (int i = 0; i < backSum; i++) {
            boolean flag = true;
            //随机数的范围为0-list.size()-1
            int target = random.nextInt(notesList.size());
            //判断是否已产生此随机数
            //若已有此随机数则跳出循环
            for (int j = 0; j < temp.length; j++) {
                if (target == temp[j]) {
                    flag = false;
                    break;
                } else {
                    temp[j] = target;
                }
            }
            //如果没有此随机数
            //将对象保存到集合中
            if (flag = true) {
                notesBack.add(notesList.get(target));
            }
        }
        //将信息存入作用域中
        model.addAttribute("userLogin", userLogin);
        model.addAttribute("userTable", userTable);
        model.addAttribute("notes", notes);
        model.addAttribute("noteAuthor", noteAuthor);
        model.addAttribute("notesBack", notesBack);

        return "noteinfo/noteinfo";
    }

    /**
     * 判断是否为达人文章
     * 首先得要是达人发表的文章
     * 其次浏览量*0.2+评论数0.3*+收藏数*0.5超1000
     *
     * @param id
     * @return
     */
    @RequestMapping("/judge")
    public String judge(Model model, HttpServletRequest req, HttpSession session) {
        //获取已登录session作用域中的
        UserTable userLogin = null;
        userLogin = (UserTable) session.getAttribute("userId");
        Integer integer = null;
        if (userLogin != null) {
            integer = userLogin.getId();
        }
        Integer id = Integer.valueOf(req.getParameter("id"));

        NotesInfo notesInfo = noteInfoService.findconById(id);

        String str = notesInfo.getContent();
        String str1=null;
        if(str!=null&&str.length()>0) {
//        String str1=str.split("<div>人好景好 </div>")[0];//截取之前的字符串
            str1 = str.substring(0, str.indexOf("@"));//截取@之前的字符串
            System.err.println(str1);
            System.out.println("//////////////////////////////////////////////");
            System.out.println(str);
        }
        //通过获取的id获取游记信息
        Notes notes = noteInfoService.findNotesById(id);
        model.addAttribute("notes", notes);

        //创建集合存储文章
        List<NotesInfo> notesInfos = new ArrayList<>();

        //获取游记作者id
        Integer authorId = notes.getAutuorId();
        //通过游记作者id获取游记作者信息
        NoteAuthor noteAuthor = noteInfoService.findNoteAuthorById(authorId);
        //获取游记作者的用户id
        Integer authorUserId = noteAuthor.getUserId();
        //如果是游记的作者查看收费游记,则直接显示全文
        if (integer != null && integer == authorUserId) {
//            notesInfos = noteInfoService.findAllById(id);
//            model.addAttribute("notesInfos", notesInfos);
            model.addAttribute("notesInfos",str);
            model.addAttribute("temp", 0);
        } else {
            //判断是否为达人文章 - 只有达人文章才能够收费
            if (notes.getNoteType() == 1) {
                //这里需要判断当前文章是否为收费文章
                //这里暂时定为浏览量*0.2+评论数0.3*+收藏数*0.5超1000
                Double num = notes.getBrowse() * 0.2 + notes.getCommentsum() * 0.3 + notes.getSupport() * 0.5;
                if (num >= 0) {
                    //判断当前用户是否登录 - 未登录直接部分读取文章
                    if (integer != null) {
                        //从订单表中查询已登录的用户是否已经购买此文章
                        Integer ordId = null;
                        ordId = notePayService.findOrderIdById(integer, id);
                        //已购买 - 查询所有的文章信息
                        if (ordId != null) {
                            //存储文章信息
//                            notesInfos = noteInfoService.findAllById(id);
                            //将文章信息存储到作用域
//                            model.addAttribute("notesInfos", notesInfos);
                            model.addAttribute("notesInfos",str);
                            //定义一个temp - 用来表示是否收费
                            model.addAttribute("temp", 0);
                        } else {
//                            notesInfos = noteInfoService.findPartById(id);
//                            model.addAttribute("notesInfos", notesInfos);
                            model.addAttribute("notesInfos",str1);
                            model.addAttribute("temp", 1);
                        }

                    } else {
//                        notesInfos = noteInfoService.findPartById(id);
//                        model.addAttribute("notesInfos", notesInfos);
                        model.addAttribute("notesInfos",str1);
                        model.addAttribute("temp", 1);
                    }
                } else {
//                    notesInfos = noteInfoService.findAllById(id);
//                    model.addAttribute("notesInfos", notesInfos);
                    model.addAttribute("notesInfos",str);
                    model.addAttribute("temp", 0);

                }
            } else {
//                notesInfos = noteInfoService.findAllById(id);
//                model.addAttribute("notesInfos", notesInfos);
                model.addAttribute("notesInfos",str);
                model.addAttribute("temp", 0);

            }
        }

        return "noteinfo/contentinfo";

    }

//    @RequestMapping("/all")
//    public String all(Model model, HttpServletRequest req) {
//        Integer ids = Integer.valueOf(req.getParameter("id"));
//        //通过获取的id获取游记信息
//        String str = noteInfoService.findAllById(ids);
//        System.out.println(str);
//        model.addAttribute("str", str);
//        model.addAttribute("temp", 0);
//        return "noteinfo/contentinfo";
//
//    }
}
